package com.holyn.guofang.view.my;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.wo.AttachedCardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CouponInfoWo;
import com.holyn.guofang.utils.HtmlUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Holyn on 2017/8/28.
 */

public class MyRechargeAdapter extends BaseSectionQuickAdapter<MyRechargeSection, BaseViewHolder>{
    private View footerAttache;
    private TextView tvGetAttache;
    private OnGetAttacheClickListener onGetAttacheClickListener;
    private RecyclerView rvAttache;
    private AttachedCardAdapter attachedCardAdapter;
    private List<AttachedCardDictionaryWo.DataBean> attacheList = new ArrayList<>();

    private View footerCoupon;
    private TextView tvGetCoupon;
    private OnGetCouponClickListener onGetCouponClickListener;
    private RecyclerView rvCoupon;
    private CouponInfoAdapter couponInfoAdapter;
    private List<CouponInfoWo.DataBean> couponList = new ArrayList<>();


    public MyRechargeAdapter(Context context, List<MyRechargeSection> data) {
        super(R.layout.item_my_recharge, R.layout.item_my_recharge_section, data);
        initFooterAttacheView(context);
        initFooterCouponView(context);

        addFooterView(footerAttache);
        addFooterView(footerCoupon);
    }

    private void initFooterAttacheView(Context context){
        footerAttache = LayoutInflater.from(context).inflate(R.layout.footer_my_recharge_attache_card, null);
        tvGetAttache = (TextView) footerAttache.findViewById(R.id.tv_get_attache);
        tvGetAttache.setText(HtmlUtil.fromHtml("<u>"+"获取附属卡"+"</u>"));
        tvGetAttache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardDictionaryWo.DataBean curCheckMainCard = getCheckedMainCard();
                Integer parentID = 0;
                if (curCheckMainCard != null){
                    parentID = curCheckMainCard.getId();
                }
                onGetAttacheClickListener.onGetAttacheClick(parentID);
            }
        });
        rvAttache = (RecyclerView) footerAttache.findViewById(R.id.rv_list);
        attachedCardAdapter = new AttachedCardAdapter(attacheList);
        rvAttache.setLayoutManager(new GridLayoutManager(context, 2));
        rvAttache.setAdapter(attachedCardAdapter);
    }

    private void initFooterCouponView(Context context){
        footerCoupon = LayoutInflater.from(context).inflate(R.layout.footer_my_recharge_coupon_info, null);
        tvGetCoupon = (TextView) footerCoupon.findViewById(R.id.tv_get_coupon);
        tvGetCoupon.setText(HtmlUtil.fromHtml("<u>"+"申请优惠"+"</u>"));
        tvGetCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttachedCardDictionaryWo.DataBean curCheckAttachedCard = getCheckedAttachedCard();
//                Integer type = 1;//购卡类型(1:购买主卡，2:只购买附属卡)
//                onGetCouponClickListener.onGetCouponClick();
            }
        });
        rvCoupon = (RecyclerView) footerCoupon.findViewById(R.id.rv_list);
        couponInfoAdapter = new CouponInfoAdapter(couponList);
        rvCoupon.setLayoutManager(new LinearLayoutManager(context));
        rvCoupon.setAdapter(couponInfoAdapter);
    }

    @Override
    protected void convertHead(BaseViewHolder baseViewHolder, MyRechargeSection myRechargeSection) {
        baseViewHolder.setText(R.id.tv_title_name, myRechargeSection.header);
        if (myRechargeSection.isShowDivide()){
            baseViewHolder.setVisible(R.id.ll_divide, true);
        } else {
            baseViewHolder.setVisible(R.id.ll_divide, false);
        }
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, MyRechargeSection myRechargeSection) {
        boolean isCheck = myRechargeSection.isCheck();
        ImageView ivCard = baseViewHolder.getView(R.id.iv_card);
        if (isCheck){
            ivCard.setImageResource(R.drawable.ic_card1_pre);
        } else {
            ivCard.setImageResource(R.drawable.ic_card1_nor);
        }
        ivCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!isCheck){
//                    for (MyRechargeSection section : getData()){
//                        section.setCheck(false);
//                    }
//                    getData().get(baseViewHolder.getAdapterPosition()).setCheck(true);
//                    notifyDataSetChanged();
//                }
                for (MyRechargeSection section : getData()){
                    section.setCheck(false);
                }
                getData().get(baseViewHolder.getAdapterPosition()).setCheck(!isCheck);
                notifyDataSetChanged();
            }
        });

        baseViewHolder.setText(R.id.tv_price, "¥"+myRechargeSection.t.getPrice()+"元");
    }

    public void setOnGetAttacheClickListener(OnGetAttacheClickListener onGetAttacheClickListener) {
        this.onGetAttacheClickListener = onGetAttacheClickListener;
    }

    public void setOnGetCouponClickListener(OnGetCouponClickListener onGetCouponClickListener) {
        this.onGetCouponClickListener = onGetCouponClickListener;
    }

    public void addAttachedCardData(List<AttachedCardDictionaryWo.DataBean> data){//添加附属卡
        attacheList.clear();
        attacheList.addAll(data);
        attachedCardAdapter.notifyDataSetChanged();
    }

    public void addCouponInfoData(List<CouponInfoWo.DataBean> data){//添加优惠卡
        couponList.clear();
        couponList.addAll(data);
        couponInfoAdapter.notifyDataSetChanged();
    }

    public CardDictionaryWo.DataBean getCheckedMainCard(){
        for (MyRechargeSection myRechargeSection : getData()){
            if (myRechargeSection.isCheck()){
                return myRechargeSection.t;
            }
        }
        return null;
    }

    public AttachedCardDictionaryWo.DataBean getCheckedAttachedCard(){
        return attachedCardAdapter.getCheckedAttachedCard();
    }

    public interface OnGetAttacheClickListener{
        void onGetAttacheClick(Integer parentID);
    }

    public interface OnGetCouponClickListener{
        void onGetCouponClick(Integer type, Integer cardCount);
    }

    private class AttachedCardAdapter extends BaseQuickAdapter<AttachedCardDictionaryWo.DataBean, BaseViewHolder>{
        private List<Boolean> isCheckList = new ArrayList<>();

        public AttachedCardAdapter(@Nullable List<AttachedCardDictionaryWo.DataBean> dataList) {
            super(R.layout.footer_item_my_recharge_attache_card, dataList);
            for (AttachedCardDictionaryWo.DataBean data : dataList){
                isCheckList.add(false);
            }
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, AttachedCardDictionaryWo.DataBean dataBean) {
            boolean isCheck = isCheckList.get(baseViewHolder.getAdapterPosition());
            ImageView ivCard = baseViewHolder.getView(R.id.iv_card);
            if (isCheck){
                ivCard.setImageResource(R.drawable.ic_card1_pre);
            } else {
                ivCard.setImageResource(R.drawable.ic_card1_nor);
            }
            ivCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < isCheckList.size(); i++){
                        isCheckList.set(i, false);
                    }
                    isCheckList.set(baseViewHolder.getAdapterPosition(), !isCheck);
                    notifyDataSetChanged();
                }
            });

            baseViewHolder.setText(R.id.tv_price, "¥"+dataBean.getPrice()+"元");
        }

        public AttachedCardDictionaryWo.DataBean getCheckedAttachedCard(){
            for (int i = 0; i<isCheckList.size(); i++){
                if (isCheckList.get(i)){
                    return getData().get(i);
                }
            }
            return null;
        }
    }

    private class CouponInfoAdapter extends BaseQuickAdapter<CouponInfoWo.DataBean, BaseViewHolder>{
        public CouponInfoAdapter(@Nullable List<CouponInfoWo.DataBean> data) {
            super(R.layout.footer_item_my_recharge_coupon_info, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, CouponInfoWo.DataBean dataBean) {
            baseViewHolder.setText(R.id.tv_rate, "折扣"+dataBean.getDiscountRate()+"%");
        }
    }
}
